package com.applink.sell_proof.builder;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.applink.sell_proof.BasePagerActivity;
import com.applink.sell_proof.ProgramDetail;
import com.applink.sell_proof.R;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.constant.IntentKey;
import com.applink.sell_proof.model.Program;

public class ProgramBuilder extends AbstractListBuilder implements OnItemClickListener {

	public ProgramBuilder(Context context, ViewGroup parent) {
		super(context, parent);
		((ListView) parent.findViewById(android.R.id.list)).setOnItemClickListener(this);
	}

	@Override
	public void notifyDataChange() {
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void buildData() {
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		int quantity_header = ((ListView) parent).getHeaderViewsCount();
		Program program = (Program) mData.get(position - quantity_header);
		Intent intent = new Intent(mContext, ProgramDetail.class);
		int indexActivity = ((BasePagerActivity) mContext).getActivityIndex();
		intent.putExtra(IntentKey.ACTIVITY_NAME, indexActivity);
		intent.putExtra(Constant.INTENT_PROGRAM_DETAIL, program);
		mContext.startActivity(intent);
	}

	@Override
	public View buildConvertView() {
		View layout = mLayoutInflater.inflate(R.layout.program_item, null);
		return layout;
	}

	@Override
	public ConcurrentHashMap<Integer, View> buildConvertViewContent(View base) {
		ConcurrentHashMap<Integer, View> views = new ConcurrentHashMap<Integer, View>();

		views.put(R.id.program_name, base.findViewById(R.id.program_name));
		views.put(R.id.program_description, base.findViewById(R.id.program_description));
		views.put(R.id.program_point, base.findViewById(R.id.program_point));
		views.put(R.id.program_qualify, base.findViewById(R.id.program_qualify));

		return views;
	}

	@Override
	public void builConvertViewContentData(int position, ConcurrentHashMap<Integer, View> views, List<? extends Object> data) {

		Program program = (Program) mData.get(position);

		((TextView) views.get(R.id.program_name)).setText(program.getName());

		((TextView) views.get(R.id.program_description)).setText(program.getDescription());
		((TextView) views.get(R.id.program_point)).setText(Integer.toString(program.getPoint()));

		String startDate = DateFormat.getDateFormat(mContext).format(new Date(program.getStartdate()));
		String endDate = DateFormat.getDateFormat(mContext).format(new Date(program.getEnddate()));

		((TextView) views.get(R.id.program_qualify)).setText(String.format("%s - %s", startDate, endDate));
	}

}
